iT邦幫忙

2025 iThome 鐵人賽

DAY 26
0
Modern Web

Laravel 是甚麼系列 第 26

讀檔

  • 分享至 

  • xImage
  •  

讀CSV檔案
產生推播
產生通知按鈕:
https://ithelp.ithome.com.tw/upload/images/20251006/20119035KmUrSe7dzE.png
用指令建立推播
php artisan make:notification OrderDelivery
https://ithelp.ithome.com.tw/upload/images/20251006/20119035i4uQrtyH1D.png

看到新增的資料夾跟內容
https://ithelp.ithome.com.tw/upload/images/20251006/201190351kO9WNAlfx.png
原來的程式碼

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class OrderDelivery extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

修改return ['mail'];
變成return ['database'];
https://ithelp.ithome.com.tw/upload/images/20251006/20119035u3uh8MhT0o.png

再修改程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20119035lWW1gRPRX0.png
變成 return [
'msg' => '訂單已送達'
];

https://ithelp.ithome.com.tw/upload/images/20251006/201190353c23amg0RG.png

用指令產生資料表的結構
php artisan notifications:table
https://ithelp.ithome.com.tw/upload/images/20251006/20119035ZbLg4PU5jh.png

使用uuid使用當下的時間搓當作id
用指令產生資料表php artisan migrate
https://ithelp.ithome.com.tw/upload/images/20251006/20119035YutMzpPIHT.png
修改OrderController的程式碼,加入
https://ithelp.ithome.com.tw/upload/images/20251006/201190358r0rVjpcxz.png
設定送出 跟 沒有送出
新增後:
https://ithelp.ithome.com.tw/upload/images/20251006/20119035kXCMhNde1h.png
看user的models裡面有預設的
use Illuminate\Notifications\Notifiable;
https://ithelp.ithome.com.tw/upload/images/20251006/20119035T4CQoQJiZG.png

回到OrderDelivery裡面也加入
use App\Notifications\OrderDelivery;

https://ithelp.ithome.com.tw/upload/images/20251006/20119035dWXDivuNib.png
再修改裡面的程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20119035GcLKmfBdQy.png
增加路由
https://ithelp.ithome.com.tw/upload/images/20251006/20119035yDEwZ3sre5.png

先用POSTMAN測試
先在後端啟動php artisan serve
https://ithelp.ithome.com.tw/upload/images/20251006/20119035iqP6KoZWmh.png
用POSTMAN測試:
https://ithelp.ithome.com.tw/upload/images/20251006/20119035GGeCrcpgUm.png
對應看資料表5
https://ithelp.ithome.com.tw/upload/images/20251006/20119035Le9Cgmy7IS.png
+
https://ithelp.ithome.com.tw/upload/images/20251006/20119035QWDiNn6vM8.png
多出現
https://ithelp.ithome.com.tw/upload/images/20251006/201190359H4BsMy92D.png
再設定
https://ithelp.ithome.com.tw/upload/images/20251006/20119035XcICAw27VB.png
改WebController 程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20119035sN1u22S0zI.png

顯示在前端https://getbootstrap.com/
加入bootstrap
https://ithelp.ithome.com.tw/upload/images/20251006/20119035wFQawDvhbZ.png
https://getbootstrap.com/docs/4.6/getting-started/introduction/
選4.6的版本
https://ithelp.ithome.com.tw/upload/images/20251006/20119035vkZX9azZg9.png
複製CSS

再貼入>顯示
https://ithelp.ithome.com.tw/upload/images/20251006/20119035Daieoo1KhM.png
將貼在
https://ithelp.ithome.com.tw/upload/images/20251006/20119035vtNB1WHEHC.pnghttps://ithelp.ithome.com.tw/upload/images/20251006/20119035QFzyNFeene.png
移到

<script src="https://code.jquery.com/jquery-3.7.1.min.js" 
        integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" 
        crossorigin="anonymous"></script>

<script>

https://ithelp.ithome.com.tw/upload/images/20251006/201190356pZqpNHlYQ.png

加入在layout的資料夾裡面>擺放的順序很重要
https://ithelp.ithome.com.tw/upload/images/20251006/20119035o1cVEDBs15.png
畫面>右鍵>檢查>看是否
https://ithelp.ithome.com.tw/upload/images/20251006/20119035WvyhOtWG13.png
搜尋modal
https://ithelp.ithome.com.tw/upload/images/20251006/20119035N1pta4Zazv.png
裡面有sample code
https://ithelp.ithome.com.tw/upload/images/20251006/20119035Qgk7OT5YIY.png
複製裡面的程式碼:

Launch demo modal

https://ithelp.ithome.com.tw/upload/images/20251006/20119035ZZcK44EtpT.png
貼上
https://ithelp.ithome.com.tw/upload/images/20251006/20119035g9Eoq8HJy1.png
改叫 通知
https://ithelp.ithome.com.tw/upload/images/20251006/201190357u3kvsiysE.png
修改
https://ithelp.ithome.com.tw/upload/images/20251006/20119035jph8suaLCD.png
再修改程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20119035n4dtyFnGmz.png
再新增檔案
https://ithelp.ithome.com.tw/upload/images/20251006/20119035qtnEJrHPfn.png
貼上
https://ithelp.ithome.com.tw/upload/images/20251006/20119035PV3ODhHdKw.png
貼上
https://ithelp.ithome.com.tw/upload/images/20251006/20119035minQlqXquD.png
修改id
https://ithelp.ithome.com.tw/upload/images/20251006/2011903576OsqePPV4.png
變 通知 把裡面先刪除
https://ithelp.ithome.com.tw/upload/images/20251006/20119035iYpiCbLMDn.png
只剩
https://ithelp.ithome.com.tw/upload/images/20251006/2011903524PBxXnt39.png
資料表裡面有msg
https://ithelp.ithome.com.tw/upload/images/20251006/20119035T0Sgqdrwvu.png
修改程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20119035VlcW3UBHLY.png

<div class="modal fade" id="notifications" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">通知</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
    
        <ul>
          @foreach ($notifications as $notification)
              <li >{{ $notification->data['msg'] }}</li>
          @endforeach
        </ul>

      </div>
    
    </div>
  </div>
</div>

畫面上面按 通知
https://ithelp.ithome.com.tw/upload/images/20251006/20119035DeZf8kNIz7.png
顯示
https://ithelp.ithome.com.tw/upload/images/20251006/201190351RGr7tDhGb.png
大家明天見~


上一篇
做每頁畫面都相同的導航列partial_view
下一篇
通知按鈕
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言